#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/slab.h>

struct pcb {
	volatile int state;
	void *stack;
	atomic_t usage;
	unsigned int flags;
	struct list_head tasks;
};

static int insert_task(struct task_struct *p) {
	struct pcb pcb1;
#if 1
	pcb1 = (struct pcb *)kmalloc(sizeof(struct pcb), GFP_KERNEL);
	if (NULL == pcb1) {
		printk("<0> kmalloc failed!\n");
	}
#endif
	pcb1->state = 1;
	pcb1->flags = 1UL;
	list_add(&(pcb1->tasks), &p->tasks);
	return 0;
}

static int rm_task(struct task_struct *p){
	struct task_struct *del = p;
	list_del(&p->tasks);
//	kfree(del);
	return 0;
}
#if 1
static int print_pid(void) {
	struct task_struct *task = NULL;
	struct task_struct *p = NULL;
	struct list_head *pos = NULL;
	int count = 0;
	
	printk("Search for insert task-------->\n");
	task = &init_task;
	list_for_each(pos, &task->tasks) {
		p = list_entry(pos, struct task_struct, tasks);
		count++;
		if (0 == p->pid) {
//			rm_task(p);
		}
		printk("state: %d, flags: %u\n", p->state, p->flags);
	}
	insert_task(p);
	printk("the number of process is %d\n", count);
	count = 0;
	printk("show all tasks-------->\n");
	task = &init_task;
	list_for_each(pos, &task->tasks) {
		p = list_entry(pos, struct task_struct, tasks);
		count++;
		printk("state: %d, flags: %u\n", p->state, p->flags);
	}
	printk("the number of process is %d\n", count);
	return 0;
}
#endif

static int __init hello_init(void) {
	printk("<1> Hello World\n");
	print_pid();
	return 0;
}

static void __exit hello_exit(void) {
	printk("<1> Goodbey\n");
	return ;
}

MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
